import {Component} from '@angular/core';

import {NavController, ViewController, AlertController} from 'ionic-angular';

import {Storage} from '@ionic/storage';

import {Common} from "../../../providers/common";
import {Config} from "../../../providers/config";

import _ from "lodash";
import {CoinService} from "./coin.service";

@Component({
  selector: 'modal-ipm-coin-select-deposit',
  templateUrl: 'selectDeposit.modal.html',
  providers: [CoinService]
})
export class IpmCoinSelectDepositModal {

  items = [0];

  moa_page_name: any = "modal-ipm-coin-select-deposit";
  moa_page_explain: any = "";

  dataList = [];
  dataListSource = [];

  isSelectAll = false;

  isLoadMore = true;

  keyWord = "";

  pageIndex = 1;

  marketNo = "";
  stockClass = "";

  type = "";
  title = "";

  constructor(public navCtrl: NavController,
              public common: Common,
              public viewCtrl: ViewController,
              public storage: Storage,
              public coinService: CoinService,
              public alertCtrl: AlertController,
              public config: Config) {
    this.marketNo = this.viewCtrl.getNavParams().get("marketNo");
    this.stockClass = this.viewCtrl.getNavParams().get("stockClass");

    this.type = this.viewCtrl.getNavParams().get("type");
    if (this.type === "bank") {
      this.title = "选择存款行";
    } else {
      this.title = "选择存款";
    }

    this.moa_page_explain = "货币基金试算 - " + this.title;
  }

  updateList() {
    if (this.keyWord) {
      this.dataList = _.filter(this.dataList, data => {
        return data["entry"]["province_city"].indexOf(this.keyWord) > -1;
      });
    } else {
      this.dataList = this.dataListSource;
    }
  }

  selectAll() {
    if (this.isSelectAll) {
      _.forEach(this.dataList, fund => {
        fund["selected"] = true;
      });
    } else {
      _.forEach(this.dataList, fund => {
        fund["selected"] = false;
      });
    }
  }


  ionViewDidEnter() {
    if (this.type === "bank") {
      this.loadListData(this.pageIndex, null, null);
    } else {
      this.loadMoneyListData();
    }
  }

  loadListData(page, infiniteScroll, refresher) {
    this.coinService.getDeposit(this.keyWord, page).subscribe(stockNoList => {

      if (stockNoList) {

        // 如果加载第一页，则清空数据
        if (page == 1) {
          this.isLoadMore = true;
          this.dataList = [];
        }

        this.dataList = _.concat(this.dataList, stockNoList);

        // 增加搜索索引
        this.pageIndex = this.pageIndex + 1;
        // 如果返回数据少于请求页数，则不再加载更多
        if (stockNoList.length < this.config.pageSize) {
          this.isLoadMore = false;
        }
      } else {
        this.dataList = [];
        this.isLoadMore = false;
      }

      // complete event
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
      if (refresher) {
        refresher.complete();
      }
    });
  }

  loadMoneyListData() {
    let fundCode = this.viewCtrl.getNavParams().get("fundCode");
    this.coinService.getDepositMoney(fundCode).subscribe(datas => {
      console.log("getDepositMoney().datas", datas);
      if (datas) {
        this.dataList = datas;
        this.setDataGridHeight();
      } else {
        this.isLoadMore = false;
      }
    });
  }

  closeModal() {
    this.viewCtrl.dismiss("close");
  }

  search() {
    this.pageIndex = 1;
    this.loadListData(this.pageIndex, null, null);
  }

  radioSelected(data) {
    this.viewCtrl.dismiss(data);
  }

  /**
   * [doInfinite 加载更多]
   * @param {[type]} infiniteScroll [事件]
   */
  doInfinite(infiniteScroll) {
    if (this.pageIndex != 1) {
      if (this.isLoadMore) {
        this.loadListData(this.pageIndex, infiniteScroll, null);
      } else {
        infiniteScroll.complete();
      }
    }
  }

  /**
   * [doRefresh 下拉刷新]
   * @param {[type]} refresher [事件]
   */
  doRefresh(refresher) {
    this.pageIndex = 1;
    this.isLoadMore = true;
    this.loadListData(this.pageIndex, null, refresher);
  }

  setDataGridHeight() {
    let row = this.dataList.length;
    // console.log(row);
    if (document.getElementById("GridTitle_Deposit")) {
      document.getElementById("GridTitle_Deposit").style.width = "1000px";
    }
    if (document.getElementById("gridRight_Deposit")) {
      document.getElementById("gridRight_Deposit").style.height = 60 * (row + 1) + 60 + "px";
    }

  }
}

